package com.analy.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.analy.constant.SystemConstant;
import com.analy.core.HeartThread;
import com.api.service.AnalyService;
import com.api.service.ManageService;
import com.common.system.SystemData;
import com.common.utils.PropsLoader;
import com.common.utils.XmlConfigUtil;
import com.common.vo.SnatchGame;
import com.common.vo.SnatchItem;
import com.common.vo.WebConfig;
import com.common.vo.WebNode;


/**
 * @author ltj 节点管理系统初始化类
 */
@Component
public class SystemMgr {
	private static Logger logger = Logger.getLogger(SystemMgr.class.getName());
	@Autowired
	private PropsLoader propsLoader;
	@Autowired
	AnalyService analyService;
	@Autowired
	HeartThread heartThread;
	
	private static final boolean TEST = true;
	
	/**
	 * 初始化的数据
	 */
	@PostConstruct
	public void init() {
		initCfgs();
		initHeart();
		if(!TEST){
			//initIce();
			//nodeRegister();// 节点注册
			//heartbeat();// 心跳接口
			initTask();
			
		}
		
		/*initDatas();
		initJms();
		initDaemon();*/
	}

	/**
	 * 销毁系统级别的资源
	 */
	@PreDestroy
	public void destroy() {
		/*// 销毁ICE客户端
		nodeManageIntegration.destoryAllIceClient();
		// 销毁ICE服务器
		IceServerUtil.destory();*/
	}

	/**
	 * 初始化的守护线程
	 */
	private void initDaemon() {
		/*clearDataDaemon.start();// 清理过期数据后台线程
		logger.info("清理过期数据线程线程启动成功");

		removeNodeDaemon.start();// 移除问题抓取节点后台线程
		logger.info("移除问题抓取节点后台线程线程启动成功");

		sendGQThirdRatioDaemon.start();
		sendSPThirdRatioDaemon.start();
		sendBFThirdRatioDaemon.start();
		
		sendGQAutoSysRatioDaemon.start();// 启动第三方赔率发送给铺货线程
		sendSPAutoSysRatioDaemon.start();

		findTradeItemIdDaemon.start();// 主动去数据中心获取交易项id
		
		sendDataDaemon.start();*/
		logger.info("主动去数据中心获取交易项id线程启动成功");

	}

	/**
	 * 初始化的 ICE服务（节点管理中心对外提供的ICE服务）
	 *//*
	private void initIce() {
		// 启动ICE服务
		Map<String, Ice.Object> srvObjMap = new HashMap<String, Ice.Object>();
		srvObjMap.put(propsLoader.props.getProperty("node.iceServer.objKey"), nodeZreocSrv);
		IceServerUtil.startIceServer(srvObjMap, SystemConstant.ICECONFG_SERVER);
		
		nodeIntegration.initIceClient();
	}*/

	/**
	 * 初始化的系统配置
	 */
	private void initCfgs() {
		FileInputStream fileInputStream = null;
		try {
			ClassLoader loader = SystemMgr.class.getClassLoader();
			URL url = loader.getResource(SystemConstant.SNATCH_CONFIG);
			fileInputStream = new FileInputStream(new File(url.getFile()));
			WebConfig webConfig = XmlConfigUtil.xmlChangeNodeWeb(fileInputStream);
			SystemData.webConfig = webConfig;
									
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fileInputStream!=null){
				try {
					fileInputStream.close();
					fileInputStream = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 节点注册
	 *//*
	public void nodeRegister() {
		nodeIntegration.nodeRegister(SystemData.nodeWebs);
	}
	
	*//**
	 * 心跳接口
	 *//*
	public void heartbeat() {
		Heartbeat heartbeat = new Heartbeat();
		heartbeat.setNodeIntegration(nodeIntegration);
		heartbeat.setAccountManageService(accountManageService);
		heartbeat.setTaskService(taskService);
		String intervalTime = propsLoader.props.getProperty("node.heartbeat.intervalTime");// 发送心跳包的间隔时间
		heartbeat.setIntervalTime(Long.parseLong(intervalTime));
		heartbeat.start();
	}
	*/
	
	public void initTask(){
		for(WebNode webNode : SystemData.webConfig.getWebNodes()){
			for(SnatchGame snatchGame : webNode.getSnatchGames()){
				for(SnatchItem snatchItem : snatchGame.getSnatchItems()){
					if(snatchItem.isAuto()){
						analyService.analyTask(webNode.getWebCode(), snatchGame.getGameCode(),
								snatchItem.getItemName(), snatchItem.getItemType(), 1);
					}
					
				}
				
			}
		}
	}
	
	public void initHeart(){
		heartThread.start();
	}
}
